context("test-order_cells")

skip_not_travis <- function ()
{
  if (identical(Sys.getenv("TRAVIS"), "true")) {
    return(invisible(TRUE))
  }
  skip("Not on Travis")
}

cds <- load_a549()
set.seed(100)
test_that("order_cells error messages work", {
  skip_on_travis()
  expect_error(order_cells(cds), "No dimensionality reduction for UMAP calculated. Please run reduce_dimension with reduction_method = UMAP, cluster_cells, and learn_graph before running order_cells." )
  cds <- estimate_size_factors(cds)
  cds <- preprocess_cds(cds, num_dim = 20)
  cds <- reduce_dimension(cds)
  expect_error(order_cells(cds), "No cell clusters for UMAP calculated. Please run cluster_cells with reduction_method = UMAP and run learn_graph before running order_cells.")
  cds <- cluster_cells(cds)
  expect_error(order_cells(cds), "No principal graph for UMAP calculated. Please run learn_graph with reduction_method = UMAP before running order_cells.")
  cds <- learn_graph(cds)
  expect_error(order_cells(cds, root_cells = c("G07_B02_RT_587"), root_pr_nodes = c("Y_1")), "Please specify either root_pr_nodes or root_cells, not both.")
  expect_error(order_cells(cds, root_cells = c("hannah")), "All provided root_cells must be present in the cell data set.")
  expect_error(order_cells(cds, root_pr_nodes = c("hannah")), "All provided root_pr_nodes must be present in the principal graph.")
  expect_error(order_cells(cds), "(When not in interactive mode, either root_pr_nodes or root_cells must be provided.|No root node was chosen!)")
  expect_error(order_cells(cds, reduction_method = "tSNE"), "Currently only 'UMAP' is accepted as a reduction_method.")
})

cds <- estimate_size_factors(cds)
cds <- preprocess_cds(cds, num_dim = 20)
cds <- reduce_dimension(cds, umap.fast_sgd=FALSE)
cds <- cluster_cells(cds, cluster_method = "louvain")
cds <- learn_graph(cds)

test_that("order_cells works", {
  skip_on_travis()
  cds <- order_cells(cds, root_pr_nodes = "Y_1")
  expect_equal(max(pseudotime(cds)), 16.5, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]),  0.111, tol = 1e-2)
  cds <- order_cells(cds, root_pr_nodes = c("Y_1", "Y_10"))
  expect_equal(max(pseudotime(cds)), 9.08, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 0.111, tol = 1e-2)
  cds <- order_cells(cds, root_cells = "G07_B02_RT_587")
  expect_equal(max(pseudotime(cds)), 14.9, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 2.18, tol = 1e-2)
  cds <- order_cells(cds, root_cells = c("G07_B02_RT_587", "F06_A01_RT_598"))
  expect_equal(max(pseudotime(cds)), 8.89, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 2.18, tol = 1e-2)
})

cds <- reduce_dimension(cds, max_components = 3, umap.fast_sgd=FALSE)
cds <- cluster_cells(cds, cluster_method = "louvain")
cds <- learn_graph(cds)

test_that("order_cells works 3d", {
  skip_on_travis()
  cds <- order_cells(cds, root_pr_nodes = "Y_1")
  expect_equal(max(pseudotime(cds)), 10.0, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]),  2.04e-5, tol = 1e-6)
  cds <- order_cells(cds, root_pr_nodes = c("Y_1", "Y_10"))
  expect_equal(max(pseudotime(cds)),  10.0, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]),  2.04e-5, tol = 1e-6)
  cds <- order_cells(cds, root_cells = "G07_B02_RT_587")
  expect_equal(max(pseudotime(cds)), 13.4, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 3.6, tol = 1e-1)
  cds <- order_cells(cds, root_cells = c("G07_B02_RT_587", "F06_A01_RT_598"))
  expect_equal(max(pseudotime(cds)), 10.2, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 3.6, tol = 1e-1)
})

cds <- cluster_cells(cds, random_seed = 100)
cds <- learn_graph(cds)

test_that("order_cells works leiden", {
  skip_on_travis()
  cds <- order_cells(cds, root_pr_nodes = "Y_1")
  expect_equal(max(pseudotime(cds)), 6.35, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 1.94, tol = 1e-2)
  cds <- order_cells(cds, root_pr_nodes = c("Y_1", "Y_2"))
  expect_equal(max(pseudotime(cds)), 3.76, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 1.94, tol = 1e-2)
  cds <- order_cells(cds, root_cells = "G07_B02_RT_587")
  expect_equal(max(pseudotime(cds)), 6.35, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 1.94 , tol = 1e-2)
  cds <- order_cells(cds, root_cells = c("G07_B02_RT_587", "F06_A01_RT_598"))
  expect_equal(max(pseudotime(cds)), 4.15, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 1.94 , tol = 1e-2)
})

cds <- reduce_dimension(cds, max_components = 3, umap.fast_sgd=FALSE)
cds <- cluster_cells(cds)
cds <- learn_graph(cds)

test_that("order_cells works leiden 3d", {
  skip_on_travis()
  cds <- order_cells(cds, root_pr_nodes = "Y_1")
  expect_equal(max(pseudotime(cds)), 6.35, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]),  1.94, tol = 1e-2)
  cds <- order_cells(cds, root_pr_nodes = c("Y_1", "Y_2"))
  expect_equal(max(pseudotime(cds)), 3.76, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]),  1.94, tol = 1e-2)
  cds <- order_cells(cds, root_cells = "G07_B02_RT_587")
  expect_equal(max(pseudotime(cds)), 6.35, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 1.94, tol = 1e-2)
  cds <- order_cells(cds, root_cells = c("G07_B02_RT_587", "F06_A01_RT_598"))
  expect_equal(max(pseudotime(cds)), 4.15, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 1.94, tol = 1e-2)
})




#### TRAVIS ####


cds <- load_a549()
set.seed(100)
test_that("order_cells error messages work", {
  skip_not_travis()
  expect_error(order_cells(cds), "No dimensionality reduction for UMAP calculated. Please run reduce_dimension with reduction_method = UMAP, cluster_cells, and learn_graph before running order_cells." )
  cds <- estimate_size_factors(cds)
  cds <- preprocess_cds(cds, num_dim = 20)
  cds <- reduce_dimension(cds)
  expect_error(order_cells(cds), "No cell clusters for UMAP calculated. Please run cluster_cells with reduction_method = UMAP and run learn_graph before running order_cells.")
  cds <- cluster_cells(cds)
  expect_error(order_cells(cds), "No principal graph for UMAP calculated. Please run learn_graph with reduction_method = UMAP before running order_cells.")
  cds <- learn_graph(cds)
  expect_error(order_cells(cds, root_cells = c("G07_B02_RT_587"), root_pr_nodes = c("Y_1")), "Please specify either root_pr_nodes or root_cells, not both.")
  expect_error(order_cells(cds, root_cells = c("hannah")), "All provided root_cells must be present in the cell data set.")
  expect_error(order_cells(cds, root_pr_nodes = c("hannah")), "All provided root_pr_nodes must be present in the principal graph.")
  expect_error(order_cells(cds), paste("When not in interactive mode, either",
                                       "root_pr_nodes or root_cells must be",
                                       "provided."))
  expect_error(order_cells(cds, reduction_method = "tSNE"), "Currently only 'UMAP' is accepted as a reduction_method.")
})

cds <- estimate_size_factors(cds)
cds <- preprocess_cds(cds, num_dim = 20)
cds <- reduce_dimension(cds, umap.fast_sgd=FALSE)
cds <- cluster_cells(cds, cluster_method = "louvain")
cds <- learn_graph(cds)

test_that("order_cells works", {
  skip_not_travis()
  cds <- order_cells(cds, root_pr_nodes = "Y_1")
  expect_equal(max(pseudotime(cds)), 10.9, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 5.88, tol = 1e-2)
  cds <- order_cells(cds, root_pr_nodes = c("Y_1", "Y_10"))
  expect_equal(max(pseudotime(cds)), 10.9, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 5.05, tol = 1e-2)
  cds <- order_cells(cds, root_cells = "G07_B02_RT_587")
  expect_equal(max(pseudotime(cds)), 10.9, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 5.88, tol = 1e-2)
  cds <- order_cells(cds, root_cells = c("G07_B02_RT_587", "F06_A01_RT_598"))
  expect_equal(max(pseudotime(cds)), 10.9, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 5.88, tol = 1e-2)
})

cds <- reduce_dimension(cds, max_components = 3, umap.fast_sgd=FALSE)
cds <- cluster_cells(cds, cluster_method = "louvain")
cds <- learn_graph(cds)

test_that("order_cells works 3d", {
  skip_not_travis()
  cds <- order_cells(cds, root_pr_nodes = "Y_1")
  expect_equal(max(pseudotime(cds)), 12.7, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 0.0949, tol = 1e-3)
  cds <- order_cells(cds, root_pr_nodes = c("Y_1", "Y_10"))
  expect_equal(max(pseudotime(cds)),  12.7, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 0.0949, tol = 1e-3)
  cds <- order_cells(cds, root_cells = "G07_B02_RT_587")
  expect_equal(max(pseudotime(cds)), 13.4, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 0.0949, tol = 1e-3)
  cds <- order_cells(cds, root_cells = c("G07_B02_RT_587", "F06_A01_RT_598"))
  expect_equal(max(pseudotime(cds)), 12.7, tol = 1e-1)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 0.0949, tol = 1e-3)
})

cds <- cluster_cells(cds, random_seed = 100)
cds <- learn_graph(cds)

test_that("order_cells works leiden", {
  skip_not_travis()
  cds <- order_cells(cds, root_pr_nodes = "Y_1")
  expect_equal(max(pseudotime(cds)), 5.74, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 0.000904, tol = 1e-5)
  cds <- order_cells(cds, root_pr_nodes = c("Y_1", "Y_2"))
  expect_equal(max(pseudotime(cds)), 2.83, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 0.000904, tol = 1e-5)
  cds <- order_cells(cds, root_cells = "G07_B02_RT_587")
  expect_equal(max(pseudotime(cds)), 4.06, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 3.23, tol = 1e-2)
  cds <- order_cells(cds, root_cells = c("G07_B02_RT_587", "F06_A01_RT_598"))
  expect_equal(max(pseudotime(cds)), 3.96, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 3.23, tol = 1e-2)
})

cds <- reduce_dimension(cds, max_components = 3, umap.fast_sgd=FALSE)
cds <- cluster_cells(cds)
cds <- learn_graph(cds)

test_that("order_cells works leiden 3d", {
  skip_not_travis()
  cds <- order_cells(cds, root_pr_nodes = "Y_1")
  expect_equal(max(pseudotime(cds)), 5.74, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 0.000904, tol = 1e-5)
  cds <- order_cells(cds, root_pr_nodes = c("Y_1", "Y_2"))
  expect_equal(max(pseudotime(cds)),  2.83, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 0.000904, tol = 1e-5)
  cds <- order_cells(cds, root_cells = "G07_B02_RT_587")
  expect_equal(max(pseudotime(cds)), 4.06, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 3.23, tol = 1e-2)
  cds <- order_cells(cds, root_cells = c("G07_B02_RT_587", "F06_A01_RT_598"))
  expect_equal(max(pseudotime(cds)), 3.96, tol = 1e-2)
  expect_equal(min(pseudotime(cds)), 0)
  expect_equal(as.numeric(pseudotime(cds)[1]), 3.23, tol = 1e-2)
})


